<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;
use Validator;

class AdminController extends Controller
{
    /**
     * 商家后台登陆页显示
     */
    public function index()
    {

        return view('adminShop.index');

    }

    /**
     * 商家资料更新
     */
    public function update()
    {
    	$data = DB::table('shops')->where('id','=',session('pid'))->first();
    	return view('adminShop.shoper.edit',['data'=>$data]);
    }

    /**
     * 商家资料执行更新
     */
    public function doUpdate(Request $request)
    {

        if(!empty($request->address)) {
           
            $validator = Validator::make($request->all(), [
                    'address' => 'exists:base_area,name'
                ],[
                    'address.exists' => '地址不正确，请重新输入'
                ]);
                if ($validator->fails()) {
                    return redirect('/adminShop/update')
                                ->withErrors($validator)
                                ->withInput();
            }
            
        }      
        
        $data = $request->except(['_token','logo']);
    	// 添加字段内容
    	$data['shopstatus'] = '0';
    	// 处理文件上传操作
    	if($request->hasFile('logo')) {
    		// 文件名
    		$name = time().rand(1000,9999);
    		// 文件后缀名
    		$suffix = $request->file('logo')->getClientOriginalExtension();
    		// 文件路径
    		$dir = config('app.upload_dir');
    		// 移动文件
    		$res = $request->file('logo')->move($dir,$name.'.'.$suffix);
    		// 拼接图片路径
    		$data['logo'] = trim($dir,'.').$name.'.'.$suffix;

    	}

    	// 将数据插入shops表中
    	if(DB::table('shops')->where('id',session('pid'))->update($data)) {

    		return redirect('/adminShop/shop')->with('info','资料已经更新完成，开始经营您的店铺吧!!!');
    	}else{

    		return back()->with('info','请重新更新资料');
    	}

    }

    public function home()
    {
    	$data = DB::table('shops')->where('id','=',session('pid'))->first();
        $counts = DB::table('goods')->where('sid','=',session('pid'))->select('goodscount')->get();
        $count = 0;
        foreach($counts as $v) {
            $count += $v->goodscount;
        }
    	return view('adminShop.shoper.home',['data'=>$data,'count'=>$count]);
    }

    /**
     * 显示修改密码页面
     */
    public function modify()
    {
        $id = session('id');
        $data = DB::table('shopers')->where('id','=',$id)->first();
        return view('adminShop.shoper.modify',['data'=>$data]);
    }

    /**
     * 执行修改密码页面
     */
    public function doModify(Request $request)
    {
        $this->validate($request,[
            'shopername' => 'required',
            'password' => 'required|regex:/^\w{3,12}$/|same:repassword'
        ],[
            'shopername.required' => '店家名称必填',
            'password.required' => '密码未填写',
            'password.regex' => '密码格式不正确',
            'password.same' => '俩次密码不一致'
        ]);

        $data = $request->except(['_token','repassword','id']);
        $data['password'] = Hash::make($data['password']);

        $Oshopername = DB::table('shopers')->where('id','=',$request->id)->value('shopername');
        $Nshopername = $data['shopername'];

        if($Oshopername != $Nshopername) {
            $request->session()->forget('shopername');
            session(['shopername' => $Nshopername]);
        }

        $res = DB::table('shopers')->where('id','=',$request->id)->update($data);
        
        if($res) {
            return redirect('/adminShop/logout')->with('info','密码更新成功，请重新登陆');
        }else{
            return back()->with('info','密码更新失败');
        }        
    }
}
